package com.globalwave.pos.sale.web;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.globalwave.base.BaseAction;
import com.globalwave.common.Convertor;
import com.globalwave.common.cache.CodeHelper;
import com.globalwave.customer.entity.CustomerProfile;
import com.globalwave.customer.service.CustomerProfileBO;
import com.globalwave.pos.inventory.entity.ProductItemSO;
import com.globalwave.pos.payment.entity.PaymentLine;
import com.globalwave.pos.sale.entity.SalesOrder;
import com.globalwave.pos.sale.entity.SalesOrderSO;
import com.globalwave.pos.sale.service.SalesOrderBO;
import com.globalwave.system.web.annotations.Pid;
import com.opensymphony.xwork2.Preparable;


@Service("pos_salesOrderAction")
@Scope("prototype")
public class SalesOrderAction extends BaseAction implements Preparable {

    private static final long serialVersionUID = 8582051849254108462L;
    
    
    private SalesOrderBO salesOrderBO ;
    private SalesOrder salesOrder ;
    private SalesOrderSO salesOrderSO ;
    private ProductItemSO productItemSO ;
    private Integer quantity ;
    private CustomerProfileBO customerProfileBO ;
    private Long cid ;
    

    @Pid(value=1000)
    public String addItem()  throws Exception {              
        
    	SalesOrder order = (SalesOrder)getSession().getAttribute(SalesOrder.SESSION_ID) ;
    	    	
    	boolean isNotExist = (order == null) ;
    	
    	order = salesOrderBO.addItem(order, productItemSO) ;
    	
    	if (isNotExist) {
    		getSession().setAttribute(SalesOrder.SESSION_ID, order) ;
    	}

        renderXML(this.getOrderMessage(order, "").asXML()) ;
        
        return null;        
    }

    public String getPaymentMethods()  throws Exception {

    	SalesOrder order = (SalesOrder)getSession().getAttribute(SalesOrder.SESSION_ID) ;
    	
        renderXML(
        		this.getPaymentMethods(
        				salesOrderBO.getPayementMethods(order), "").asXML()) ;
        
    	return null ;
    }
    
    @Pid(value=1000)
    public String payAndAutoComplete() throws Exception {
        
    	SalesOrder order = (SalesOrder)getSession().getAttribute(SalesOrder.SESSION_ID) ;
    	SalesOrder payOrder = new SalesOrder() ;
    	payOrder.setPaymentLines((List)Convertor.xml2List(super.getParamsAsXml(), PaymentLine.class)) ;
    	salesOrderBO.payAndAutoComplete(order, payOrder) ;

        renderXML(this.getOrderMessage(order, "").asXML()) ;
        
        clear();
        
    	return null ;
    }
    
    public String clear() throws Exception {
    	
    	getSession().removeAttribute(SalesOrder.SESSION_ID) ;
    	
    	return null ;
    }

    public Element getOrderMessage(SalesOrder order,String msg) throws Exception {

        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("UTF-8");
        Element root = document.addElement("root") ;
        root.addElement("message").addText(msg) ;
        
        if (order != null) {
        	Element eOrder = Convertor.object2Xml(order) ;
        	if (order.getLines() != null) {
        	    eOrder.add(Convertor.list2Xml(order.getLines(), "lines")) ;
        	}
        	if (order.getPaymentLines() != null) {
        	    eOrder.add(Convertor.list2Xml(order.getPaymentLines(), "paymentLines")) ;
        	}
        	eOrder.remove(eOrder.element("subscriber")) ;
        	eOrder.remove(eOrder.element("consignee")) ;
        	if (order.getSubscriber() != null) {
        	    eOrder.add(Convertor.object2Xml(order.getSubscriber(), "subscriber")) ;
        	}
        	if (order.getConsignee() != null) {
        	    eOrder.add(Convertor.object2Xml(order.getConsignee(), "consignee")) ;
        	}
        	root.addElement("data").add(eOrder);
        }
        
        root.addElement("versionId").addText(String.valueOf(CodeHelper.getVersionId())) ;
        
        return root ;
             
    }

    public Element getPaymentMethods(List<PaymentLine> paymentLines,String msg) throws Exception {

        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("UTF-8");
        Element root = document.addElement("root") ;
        
        root.addElement("message").addText(msg) ;
        root.add(Convertor.list2Xml(paymentLines, "paymentLines"));
        
        
        return root ;
             
    }

    @Pid(value=1000)
    public String getSubscriber()  throws Exception {              

        this.salesOrder = salesOrderBO.newOrder() ;
		getSession().setAttribute(SalesOrder.SESSION_ID, this.salesOrder) ;
		
		CustomerProfile customerProfile = (CustomerProfile)getCustomerProfileBO().get(cid) ;
        this.salesOrder.setSubscriber_number(customerProfile.getCid()) ;
        this.salesOrder.setSubscriber_type(customerProfile.getType_()) ;
        //this.salesOrder.setSubscriber(customerProfile) ;

        renderXML(this.getMessage(customerProfile, "").asXML()) ;
        
        return null;    
        
    }

    public String getConsignee()  throws Exception {              

        this.salesOrder = (SalesOrder) getSession().getAttribute(SalesOrder.SESSION_ID) ;
		
		CustomerProfile customerProfile = (CustomerProfile)getCustomerProfileBO().get(cid) ;
        this.salesOrder.setConsignee_number(customerProfile.getCid()) ;
        //this.salesOrder.setConsignee(customerProfile) ;

        renderXML(this.getMessage(customerProfile, "").asXML()) ;
        
        return null;    
        
    }

    public String query()  throws Exception {
    	
        renderXML(salesOrderBO.query(salesOrderSO, pageIndex, pageSize, null).asXML()) ;
        
    	return null ;
    }
    

    public String get()  throws Exception {

    	
        renderXML(this.getOrderMessage(salesOrderBO.getWithLines(salesOrder), "").asXML()) ;
        
    	return null ;
    }

    public String cancel()  throws Exception {
    	
    	renderXML(this.getOrderMessage(salesOrderBO.cancel(this.salesOrder), "").asXML()) ;
    	
    	return null ;
    }
    

	@Override
	public void prepare() throws Exception {
		
	}
    
	public SalesOrderBO getSalesOrderBO() {
		return salesOrderBO;
	}

	public void setSalesOrderBO(SalesOrderBO salesOrderBO) {
		this.salesOrderBO = salesOrderBO;
	}


	public SalesOrder getSalesOrder() {
		return salesOrder;
	}


	public void setSalesOrder(SalesOrder salesOrder) {
		this.salesOrder = salesOrder;
	}


	public Integer getQuantity() {
		return quantity;
	}


	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public ProductItemSO getProductItemSO() {
		return productItemSO;
	}

	public void setProductItemSO(ProductItemSO productItemSO) {
		this.productItemSO = productItemSO;
	}

	public CustomerProfileBO getCustomerProfileBO() {
		if (customerProfileBO == null) {
			this.customerProfileBO = 
				(CustomerProfileBO)CodeHelper.getAppContext().getBean("customerProfileBO") ;
		}
		return customerProfileBO;
	}

	public Long getCid() {
		return cid;
	}

	public void setCid(Long cid) {
		this.cid = cid;
	}

	public SalesOrderSO getSalesOrderSO() {
		return salesOrderSO;
	}

	public void setSalesOrderSO(SalesOrderSO salesOrderSO) {
		this.salesOrderSO = salesOrderSO;
	}




    
}
